//
//  publicMethod.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/25.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class publicMethod: NSObject {
    
    //隐藏tableView多余的线条
   static func hiddleExtendCellForTableView(tableView: UITableView) -> Void {
        
        let view = UIView()
        view.backgroundColor = UIColor.clearColor()
        tableView.tableFooterView = view
        
    }
    //计算时间差值
    static func computationTimeDifference(time: String) ->String{
       
        let dateForm = NSDateFormatter()
        //dateForm.locale = NSLocale.
        dateForm.dateFormat = "yyyy-MM-dd HH:mm:ss"
        if let da = dateForm.dateFromString(time){
            let now = NSDate()
            //let da = dateForm.dateFromString(time)
            //now即为现在的时间，由于后面的NSCalendar可以匹配系统日期所以不用设置local
            let das = NSCalendar.currentCalendar()
            //new 一个 NSCalendar
            let flags: NSCalendarUnit = [NSCalendarUnit.Year,NSCalendarUnit.Month,NSCalendarUnit.Day, NSCalendarUnit.Hour,NSCalendarUnit.Minute]
            //设置格式
            let nowCom = das.components(flags, fromDate: now)
            let timeCom = das.components(flags, fromDate: da)
            //创建当前和需要计算的components
            //components有之前设置的格式的各种参数
            
            if timeCom.year == nowCom.year{
                if timeCom.month == nowCom.month {
                    if timeCom.day == nowCom.day{
                        if timeCom.hour == nowCom.hour{
                            return "\(nowCom.minute - timeCom.minute)分钟前"
                        }else{
                            return "今天 \(timeCom.hour):\(timeCom.minute)"
                        }
                    }else{
                        if nowCom.day - timeCom.day == 1{
                            return "昨天 \(timeCom.hour):\(timeCom.minute)"
                        }else{
                            return "\(nowCom.day - timeCom.day)天前"
                        }
                    }
                }else{
                    return "\(nowCom.month - timeCom.month)月前"
                }
                
            }else{
                return "\(nowCom.year - timeCom.year)年前"
            }
            

            
        }
                return time
       
    }
    //计算label高度
   static func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat {
    
        let statusLabelText: NSString = labelStr
        let size = CGSizeMake(width, 900)
        let dic = NSDictionary(object: font, forKey: NSFontAttributeName)
        let strSize = statusLabelText.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context: nil).size
        return strSize.height
   
    }
    

    //计算label宽度
   static func getLabWidth(labelStr:String,font:UIFont,height:CGFloat) -> CGFloat {
        
        let statusLabelText: NSString = labelStr
        
        let size = CGSizeMake(900, height)
        
        let dic = NSDictionary(object: font, forKey: NSFontAttributeName)
        
        let strSize = statusLabelText.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context:nil).size
        
        return strSize.width
        
    }
    //等比缩放
    static func scaleImage(image: UIImage, withScale scale: CGFloat) -> UIImage {
        UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale))
        image.drawInRect(CGRectMake(0, 0, image.size.width * scale, image.size.height * scale))
        let scaledImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return scaledImage
    }
    
    //拨打电话
    static func callPhone(phoneNumber: String, withSuperView view: UIView) {
        var phoneWebView: UIWebView?
        for subV: UIView in view.subviews {
            if (subV is UIWebView) {
                phoneWebView = (subV as! UIWebView)
                break
            }
        }
        if phoneWebView == nil {
            //如果没有的话就添加一个webView
            phoneWebView = UIWebView(frame: CGRectZero)
            view.addSubview(phoneWebView!)
        }
        //否则就不用添加了,直接用已经存在的webView来加载就可以了
        let url = NSURL(string: "tel://".stringByAppendingString(phoneNumber))!
        phoneWebView!.loadRequest(NSURLRequest(URL: url))
    }
    //将nsdata转成字符串
    static func dataFromString(date: NSDate) -> String{
        let dateFormatter = NSDateFormatter()
        
        dateFormatter.dateFormat = "yyyy-MM-dd"
        
        let result = dateFormatter.stringFromDate(date)
        
        return result
    }
    
    static func setHUDStyle() {
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.Custom)
        SVProgressHUD.setFont(UIFont.systemFontOfSize(14))
        SVProgressHUD.setForegroundColor(UIColor.whiteColor())
        SVProgressHUD.setBackgroundColor(UIColor(red: 22 / 255.0, green: 22 / 255.0, blue: 22 / 255.0, alpha: 0.7))
        SVProgressHUD.setRingThickness(1.5)
        
    }
    static func token() -> String{
        if NSUserDefaults.standardUserDefaults().stringForKey("k_UD_token") != nil{
            return NSUserDefaults.standardUserDefaults().stringForKey("k_UD_token")!
        }else{
            return ""
        }
        
    }
    static func isComplete() -> Bool{
        return NSUserDefaults.standardUserDefaults().boolForKey("k_UD_isComplete")
       
    }
    
    static func userName() ->String{
        return NSUserDefaults.standardUserDefaults().stringForKey("k_UD_user")!
    }
    
    static func passWord() ->String{
        
        return NSUserDefaults.standardUserDefaults().stringForKey("k_UD_pass")!
    
    }
    
    static func isWechat() -> Bool{
        if UIApplication.sharedApplication().canOpenURL(NSURL(string: "weixin://")!){
            return true
            
        }else{
            return false
        }
    }
   
    
    static func isQQ() -> Bool{
        if UIApplication.sharedApplication().canOpenURL(NSURL(string:"mqq://")!){
            return true
        }else{
            return false
        }
    }
    
    
    
    static func isApns() -> Bool{
        return NSUserDefaults.standardUserDefaults().boolForKey("push_of_on")
    }
    
    


}
